from django.shortcuts import render
from django.views import View
from django.http import JsonResponse
from constr_budgets.models import *
import json
from utils.Infinite import data as data1
import datetime

# Create your place here.

class RecoderView(View):
    def get(self,request):
        day_id = request.GET.get("day_id","")
        project_id = request.GET.get("project_id")
        template_id = Project.objects.get(id=project_id).models_id
        aimTemplate = Templates.objects.get(id=template_id)
        placelist = list(aimTemplate.place_set.all().values())
        moduleslist = list(aimTemplate.modules_set.all().values())
        resultlist = []
        timelist = []       #记录多条时间id

        try:
            start_day = datetime.datetime.strptime(request.GET.get("start_day",""),"%Y-%m-%d").date()
            end_day = datetime.datetime.strptime(request.GET.get("end_day",""),"%Y-%m-%d").date()
            dayList = Day.objects.all().values()
            for i in list(dayList):
                if start_day<=i["time"]<=end_day:
                    timelist.append(i['id'])
        except:
            pass
        for i in moduleslist:
            temp = []
            for j in placelist:
                try:
                    if day_id.isdigit():
                        res = list(Record.objects.filter(modules=i['id']).filter(place=j['id']).filter(day_id=day_id).values())
                        data = {}     #放止无效的数据
                        if(len(res)):
                            data = res[0]

                    elif len(timelist)>0:
                        res = list(Record.objects.filter(modules=i['id']).filter(place=j['id']).filter(day_id__in=timelist).values())
                        data = {}  # 放止无效的数据
                        if (len(res)):
                            for item in res:
                                if data == {}:
                                    data = item
                                else:
                                    data['num'] += item['num']
                                    data['money'] += item['money']

                    else:
                        res = list(Record.objects.filter(modules=i['id']).filter(place=j['id']).values())
                        data = {}  # 放止无效的数据
                        if (len(res)):
                            for item in res:
                                if data == {}:
                                    data = item
                                else:
                                    data['num'] += item['num']
                                    data['money'] += item['money']
                except:
                    return JsonResponse({"code": 400,"message":"时间数据有误"})
                temp.append(data)
            resultlist.append(temp)

        moduleslist = data1(moduleslist)
        return JsonResponse({"code":200,"recoderlist":resultlist,"placelist":placelist,"moduleslist":moduleslist})


    def put(self,request):
        data = json.loads(request.body)
        id = data['id']
        data.pop("id")
        Record.objects.filter(id=id).update(**data)
        return JsonResponse({"code": 200, "message": "修改成功"})
